From: Taku Kudo Date: Mon, 13 Jun 2022 17:00:43 +0000 (+0900) Subject: Uses std::atomic to define global variable X-Git-Tag: archive/raspbian/0.1.97-3+rpi1^2~22 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=28953460d1c4cae823b283faa606679b5a30f7ea;p=sentencepiece.git Uses std::atomic to define global variable Signed-off-by: Kentaro Hayashi Gbp-Pq: Name 0006-Uses-std-atomic-to-define-global-variable.patch --- diff --git a/src/common.h b/src/common.h index 7595634..6ec4c09 100644 --- a/src/common.h +++ b/src/common.h @@ -50,19 +50,6 @@ typedef uint32_t char32; typedef uint32_t uint32; typedef uint64_t uint64; -static constexpr uint8 kuint8max = ((uint8)0xFF); -static constexpr uint16 kuint16max = ((uint16)0xFFFF); -static constexpr uint32 kuint32max = ((uint32)0xFFFFFFFF); -static constexpr uint64 kuint64max = ((uint64)(0xFFFFFFFFFFFFFFFF)); -static constexpr int8 kint8min = ((int8)~0x7F); -static constexpr int8 kint8max = ((int8)0x7F); -static constexpr int16 kint16min = ((int16)~0x7FFF); -static constexpr int16 kint16max = ((int16)0x7FFF); -static constexpr int32 kint32min = ((int32)~0x7FFFFFFF); -static constexpr int32 kint32max = ((int32)0x7FFFFFFF); -static constexpr int64 kint64min = ((int64)(~0x7FFFFFFFFFFFFFFF)); -static constexpr int64 kint64max = ((int64)(0x7FFFFFFFFFFFFFFF)); - static constexpr uint32 kUnicodeError = 0xFFFD; #if defined(OS_WIN) && defined(UNICODE) && defined(_UNICODE) diff --git a/src/util.cc b/src/util.cc index 8da16c4..f99c73a 100644 --- a/src/util.cc +++ b/src/util.cc @@ -14,27 +14,28 @@ #include "util.h" +#include #include namespace sentencepiece { namespace { constexpr unsigned int kDefaultSeed = static_cast(-1); -static unsigned int g_seed = kDefaultSeed; -static int g_minloglevel = 0; +static std::atomic g_seed = kDefaultSeed; +static std::atomic g_minloglevel = 0; } // namespace void SetRandomGeneratorSeed(unsigned int seed) { - if (seed != kDefaultSeed) g_seed = seed; + if (seed != kDefaultSeed) g_seed.store(seed); } uint32 GetRandomGeneratorSeed() { - return g_seed == kDefaultSeed ? std::random_device{}() : g_seed; + return g_seed == kDefaultSeed ? std::random_device{}() : g_seed.load(); } namespace logging { -int GetMinLogLevel() { return g_minloglevel; } -void SetMinLogLevel(int v) { g_minloglevel = v; } +int GetMinLogLevel() { return g_minloglevel.load(); } +void SetMinLogLevel(int v) { g_minloglevel.store(v); } } // namespace logging namespace string_util {